function [fh,xh] = sa(fcn,x0,T,R,data)

nx = length(x0); 


for r=1:R 
    n=1;  
    while n<R  
        
x1 = x0+T*randn(nx,1); 
f0 = feval(fcn,x0,data);
f1 = feval(fcn,x1,data);
delta = f1-f0; 
u   =rand; 
      if u<1/(1+exp(delta/T))
         x0 = x1; 
      end
   
     n=n+1; 

    end
    
    
    T=0.9*T;
end 

xh = x0;
fh = feval(fcn,xh,data); 